package com.zxm.sunshineaiary.view.flinggallery.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.AdapterView.OnItemClickListener;

import com.zxm.sunshineaiary.view.flinggallery.adapter.FingGalleryAdapter;
import com.zxm.sunshineaiary.view.flinggallery.info.FingGalleryInfo;
import com.zxm.sunshineaiary.view.flinggallery.util.Operation;

public class FingGallery extends FrameLayout implements OnTouchListener {
	private String TAG = "FingGallery";

	public View mCurrentlyScrollView;

	private boolean onLayoutFlag = true;
	private Context mContext;
	private View[] mViews;
	private GestureDetector mGestureDetector;
	private FingGalleryAdapter mAdapter;
	private FingGalleryInfo mFGInfo;
	private Operation mOperation;
	private FlingGalleryAnimation mAnimation;
	private OnItemClickListener mItemClickListener;

	public FingGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.initView(context);
	}

	public FingGallery(Context context) {
		super(context);
		this.initView(context);
	}

	public FingGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.initView(context);
	}

	public void setAdapter(FingGalleryAdapter adapter) {

		this.mAdapter = adapter;
		this.setGalleryViewSize(adapter);
		this.mViews = adapter.getItems();
		this.addViewForLayout(mViews);

	}

	public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
		this.mItemClickListener = mOnItemClickListener;
	}

	private void initView(Context context) {
		FingGalleryInfo.initialization();
		Operation.initialization();
		this.mContext = context;
		this.mFGInfo = FingGalleryInfo.getFGInfoInstance(this);
		this.mOperation = Operation.getOperationInstatnce(this);
		this.mAnimation = new FlingGalleryAnimation(this);
	}

	private void setGalleryViewSize(FingGalleryAdapter adapter) {
		FingGalleryInfo.child_view_wide = adapter.getViewWidht();
		FingGalleryInfo.child_view_high = adapter.getViewHigh();
	}

	private void addViewForLayout(View[] views) {
		for (int i = 0; i < views.length; i++) {
			this.addView(views[i]);
		}
		setListener(views);
		setMyLayout(views);
		mGestureDetector = new GestureDetector(new FlingGestureDetector(this));
	}

	private void setMyLayout(View[] views) {
		int viewCenter = -((this.getLayoutParams().height / 2) - (FingGalleryInfo.child_view_high / 2));
		mFGInfo.setViewCenter(viewCenter);
	}

	private void setListener(View[] view) {
		for (View mView : view) {
			mView.setOnTouchListener(this);
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		this.clearAnimation();
		int mPosationState = mFGInfo.getPosation();
		mGestureDetector.onTouchEvent(event);
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			Log.v("FingGallery", "ACTION_UP");
			if (mFGInfo.isClick()) {
				if (isTouchPosationView(v, mPosationState)) {
					mItemClickListener.onItemClick(null, v, mPosationState,
							mPosationState);
				} else {
					otherItemClick(v, mPosationState);
					this.startAnimation(mAnimation);
				}
			} else {
				mOperation.setTopMargin();
				setScrollAnimation(mAnimation);
				this.startAnimation(mAnimation);
				mOperation.setClick(true);
			}
			break;
		}
		return true;
	}

	/**
	 * ��������
	 * 
	 * @param mAnimation
	 */
	protected void setScrollAnimation(FlingGalleryAnimation mAnimation) {

		int stateY = mOperation.getStateY();
		int targetY = mOperation.getDistanceY(stateY);
		int topY = stateY - targetY;
		mAnimation.setCoordinates(stateY, topY);
	}

	/**
	 * ����ť����Ӧ
	 * 
	 * @param v
	 */
	protected void otherItemClick(View v, int mPosationState) {
		int posation = getClickViewPosation(v);
		int posationPoor = (mPosationState - posation);
		int stateY = mOperation.getStateY();
		int topY = posationPoor * FingGalleryInfo.child_view_high;
		mAnimation.setCoordinates(stateY, topY);
		mFGInfo.setPosation(posation);
	}

	public View getCurrentlyScrollView() {
		return this.mCurrentlyScrollView;
	}

	public View[] getViews() {
		return mViews;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		if (onLayoutFlag) {
			int childCenter = mFGInfo.getChildCenter();
			int viewCenter = mFGInfo.getViewCenter();
			mOperation.onLayout(childCenter, viewCenter);
			onLayoutFlag = false;
		}
	}

	private boolean isTouchPosationView(View v, int mPosationState) {
		if (v == mViews[mPosationState]) {
			return true;
		}
		return false;

	}

	private int getClickViewPosation(View vies) {
		for (int i = 0; i < mViews.length; i++) {
			if (vies == mViews[i]) {
				return i;
			}
		}
		return 999;
	}

	/**
	 * 设置按钮初始居中位置
	 * 
	 * @param posation
	 */
	public void setGalleryPosation(int posation) {
		this.mFGInfo.setChildCenter(posation);
	}

}
